\documentclass[11pt]{article}
%% \topmargin -1.5cm        % read Lamport p.163
%% \oddsidemargin -0.04cm   % read Lamport p.163
%% \evensidemargin -0.04cm  % same as oddsidemargin but for left-hand pages
%% \textwidth 16.59cm
%% \textheight 21.94cm 
%$ %\pagestyle{empty}       % Uncomment if don't want page numbers
\parskip 7.2pt           % sets spacing between paragraphs
%% %\renewcommand{\baselinestretch}{1.5} % Uncomment for 1.5 spacing between lines
\parindent 0pt		 % sets leading space for paragraphs

\usepackage{fullpage}
\usepackage{listings} % For source code
\usepackage[usenames,dvipsnames]{color} % For colors and names
\usepackage[pdftex]{graphicx}
\usepackage{subfigure}

\newcommand{\superscript}[1]{\ensuremath{^{\textrm{#1}}}}
\newcommand{\subscript}[1]{\ensuremath{_{\textrm{#1}}}}

\definecolor{mygrey}{gray}{.96} % Light Grey
\lstset{ 
        language=[ISO]C++,              
        tabsize=3,                                                     
        basicstyle=\tiny,               
        numbers=left,                   
        numberstyle=\tiny,              
        stepnumber=2,                   
        numbersep=5pt,                  
        backgroundcolor=\color{white}, 
        %showspaces=false,              
        %showstringspaces=false,        
        %showtabs=false,                
        frame=single,                    
        tabsize=3,                          
        captionpos=b,                   
        breaklines=true,                
        breakatwhitespace=false,        
        %escapeinside={\%*}{*)},        
        commentstyle=\color{BrickRed}   
}

\title{ECE5666 \\
  Midterm Question 3 \\
  Paul Ozog
  \date{\today}
  \author{
    Instructor: Deniz Erdogmus \\
  }
}

\begin{document}

\section*{Paul Ozog - Question 3}
The Z-Transform of h[n] is given by the following equation:
\begin{equation}
  H(z) = G \frac{ \left ( 1 - o_1z^{-1} \right ) \left ( 1 - o_2z^{-1} \right ) ... \left ( 1 - o_Mz^{-1} \right)}{\left ( 1 - p_1z^{-1} \right ) \left ( 1 - p_2z^{-1} \right ) ... \left ( 1 - p_Nz^{-1} \right)}
\end{equation}

So I make use of the following factorization (note that because \texttt{impz} considers all inverse impulse responses to be right sided, we can emulate left-sided inverse Z-transforms by inverting the poles and zeros outside the unit circle):
\begin{equation}
  H(z) = H_L(z) H_R(z) = G \left( \frac{\prod_{i=1}^{Z_L}\left( 1 - \frac{1}{o_{i}^L}z^{-1} \right)}{\prod_{l=1}^{P_L}\left( 1 - \frac{1}{p_{l}^L}z^{-1} \right)} \right) \left(\frac{\prod_{j=1}^{Z_R} \left ( 1 - o_j^Rz^{-1} \right )}{\prod_{r=1}^{P_R}\left ( 1 - p_{r}^Rz^{-1} \right )} \right)
\end{equation}
where
\begin{math}
P_L, \; P_R, \; Z_L, \; Z_R, \; p_i^L, \; p_i^R, \; o_i^L, \; o_i^R,
\end{math}
are the number of left-sided/right-sided poles, number of left-sided/right-sided zeros, ith left pole, ith right pole, ith left zero, and ith right zero, respectively.

\texttt{inversez} considers a pole {\it p} to be left-sided if {\it p} is outside the unit circle - ie:
\begin{equation}
  \left| p \right| \geq 1
\end{equation}
and to be right sided if:
\begin{equation}
  \left| p \right| < 1
\end{equation}

In the time domain, the above factorization is equivalent to:
\begin{equation}
  h[n] = h_L[n] * h_R[n]
\end{equation}

So if a sequence
\begin{math}
  h^{-1}[n]
\end{math}
is desired, one simply takes the inverse of H(z):

\begin{equation}
  H^{-1}(z) = G^{-1} \left( \frac{\prod_{l=1}^{P_L}\left( 1 - \frac{1}{p_{l}^L}z^{-1} \right)}{\prod_{i=1}^{Z_L}\left( 1 - \frac{1}{o_{i}^L}z^{-1} \right)} \right) \left(\frac{\prod_{r=1}^{P_R}\left ( 1 - p_{r}^Rz^{-1} \right )}{\prod_{j=1}^{Z_R} \left ( 1 - o_j^Rz^{-1} \right )} \right)
\end{equation}

After checking that all poles and zeros are given in conjugate pairs, \texttt{inversez} calculates 
\begin{math}
h_{L}
\end{math}
and
\begin{math}
h_{R}
\end{math}
using \texttt{impz}:
\begin{verbatim}
  hL = fliplr(impz(leftDenom, leftNum, L+1));
  hR = impz(rightDenom, rightNum, L+1);
\end{verbatim}

Similarly, their inverses are calculated by switching the numerators and denominators of the above expressions:
\begin{verbatim}
  hLinv = fliplr(impz(leftNum, leftDenom, L+1));
  hRinv = impz(rightNum, rightDenom, L+1);
\end{verbatim}

h[n] is then determined using convolution:
\begin{verbatim}
  h    = G    * conv(hL, hR);
  hinv = G^-1 * conv(hLinv, hRinv); 
\end{verbatim}

Finally, the d[n] approximation of the Dirac delta function by convolving 
\begin{math}
  h[n]
\end{math}
and 
\begin{math}
  h^{-1}[n]:
\end{math}

\begin{verbatim}
  d = conv(h, hinv);
\end{verbatim}

Starting with a value of L = 10, \texttt{inversez} checks to see that 
\begin{equation}
  \frac{d[0]}{\sum_{n \neq 0}^{} d^2[n]} > \gamma
\end{equation}
where 
\begin{math}
  \gamma = 10^4
\end{math}

If not, L is doubled and \texttt{hL}, \texttt{hR}, \texttt{hLinv}, \texttt{hRinv}, \texttt{h}, \texttt{hinv}, and \texttt{d} are recalculated until the above condition is satisfied.
\end{document}
